﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Pavilion;

namespace Pavilion.Control
{
    public class Icon : Control
    {
        private ImagePanel image;
        public event Action<Icon> Selected;

        public Icon(Rectangle bounds) : base()
        {
            Initialize(bounds);
        }

        public Icon(Rectangle bounds, string imagePath) : base()
        {
            image = new ImagePanel(imagePath);
            _Collection.Add(image);

            Initialize(bounds);
            CordinateImagePosition();

            this.Resize += new EventHandler(Icon_Resize);
            this.image.Resize += new EventHandler(Image_Resize);
        }

        private void Initialize(Rectangle bounds)
        {
            AreaBounds = bounds;
            BackgroundColor = Color.Transparent;

            Panel panel = new Panel(new Rectangle(0, 0, AreaBounds.Width, AreaBounds.Height));
            panel.BackgroundColor = Color.Transparent;
            panel.OutlineColor = Color.Transparent;
            _Collection.Add(panel);

            panel.Input.Enter += new EventHandler(Icon_Enter);
            panel.Input.Leave += new EventHandler(Icon_Leave);
            panel.Input.MousePress += new MouseObjectHandler(Icon_MousePress);

            LeaveOutlineColor = Color.Black;
            EnterOutlineColor = Color.White;
        }

        public override void Draw()
        {
            if (!Visible)
                return;

            DrawManager.Instance.FillRectangleOnScreen(ScreenBounds, BackgroundColor);
            base.Draw();
            DrawManager.Instance.DrawRectangleOnScreen(ScreenBounds, OutlineColor);
        }

        /// <summary>
        /// Forces this icon to be selected.
        /// </summary>
        public void Select()
        {
            OnSelected();
        }

        private void CordinateImagePosition()
        {
            if (image == null)
                return;

            image.Position = new Point((this.AreaBounds.Width - image.AreaBounds.Width) / 2, (this.AreaBounds.Height - image.AreaBounds.Height) / 2);
        }

        /// <summary>
        /// Gets or sets the item of this Icon.
        /// </summary>
        public object Item { get; set; }

        /// <summary>
        /// Gets or sets the outline color for when the cursor enters the icon region.
        /// </summary>
        public Color EnterOutlineColor { get; set; }

        /// <summary>
        /// Gets or sets the outline color for when the cursor leaves the icon region.
        /// </summary>
        public Color LeaveOutlineColor { get; set; }

        /// <summary>
        /// Gets the status of whether this icon is currently selected.
        /// </summary>
        public bool IsSelected
        {
            get { return (BackgroundColor != Color.Transparent); }
        }

        private void OnSelected()
        {
            if (Selected != null)
                Selected(this);
        }

        private void Icon_MousePress(object sender, MouseCursor mouseCursor)
        {
            Select();
        }

        private void Icon_Leave(object sender, EventArgs e)
        {
            this.OutlineColor = LeaveOutlineColor;
        }

        private void Icon_Enter(object sender, EventArgs e)
        {
            this.OutlineColor = EnterOutlineColor;
        }

        private void Icon_Resize(object sender, EventArgs e)
        {
            CordinateImagePosition();
        }

        private void Image_Resize(object sender, EventArgs e)
        {
            CordinateImagePosition();
        }
    }
}
